package com.yunpai.thread;

/**
 * 模拟次窗口卖票案列  继承Thread的方式 与静态方法方式
 */
public class WindThreadTicketTypeTwo extends Thread {
    static int ticket = 100;

    @Override
    public void run() {
        while (true) {
            method();
        }
    }

    public static synchronized void method() { //同步监视器为当前的WindThreadTicketTypeTwo.classs
        if (ticket > 0)
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        System.out.println(Thread.currentThread().getName() + "卖的是第" + ticket + "张");
        ticket--;
    }

    public static void main(String[] args) {
        WindThreadTicketTypeTwo windows1 = new WindThreadTicketTypeTwo();
        WindThreadTicketTypeTwo windows2 = new WindThreadTicketTypeTwo();
        WindThreadTicketTypeTwo windows3 = new WindThreadTicketTypeTwo();
        windows1.setName("窗口一");
        windows2.setName("窗口二");
        windows3.setName("窗口三");
        windows1.start();
        windows2.start();
        windows3.start();
    }
}
